/*
 * Copyright 2018-10-17 7:49:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.core.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lap.scd.commons.core.exception.ManagerException;
import com.lap.scd.commons.page.Page;
import com.lap.scp.erp.core.ReceivablesDetailManager;
import com.lap.scp.erp.dal.ContractDAO;
import com.lap.scp.erp.dal.ReceivablesDAO;
import com.lap.scp.erp.dal.ReceivablesDetailDAO;
import com.lap.scp.erp.domain.ReceivablesDetailDO;
import com.lap.scp.erp.query.ReceivablesDetailQueryVO;

/**
 * <pre>
 * 收款明细业务代码默认实现类，这里提供最小元素原子操作，所以尽量少在该类中做流程代码。
 * 业务流程代码应该写在AO中。
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-17 7:49:26
 * @since 2.0.0
 */
@Service("receivablesDetailManager")
public class DefaultReceivablesDetailManager implements ReceivablesDetailManager {
	/**
	 * 收款明细数据操作对象
	 */
	@Resource
	private ReceivablesDetailDAO receivablesDetailDAO;

	@Resource
	private ReceivablesDAO receivablesDAO;

	@Resource
	private ContractDAO contractDAO;

	@Override
	public void insertReceivablesDetail(ReceivablesDetailDO detailDO) throws ManagerException {
		Integer contractId = detailDO.getContractId();
		Integer receivablesId = detailDO.getReceivablesId();
		Integer amount = detailDO.getAmount();
		String updatedBy = detailDO.getUpdatedBy();

		int contractFlag = contractDAO.updatePay(contractId, amount, updatedBy);
		if (contractFlag > 0) {
			int receivablesFlag = receivablesDAO.updateAmount(receivablesId, amount, updatedBy);
			if (receivablesFlag > 0) {
				receivablesDetailDAO.insertReceivablesDetail(detailDO);
			}
		}
	}

	@Override
	public List<ReceivablesDetailDO> queryPage(Page page, ReceivablesDetailQueryVO queryVO) throws ManagerException {
		int count = receivablesDetailDAO.countPage(queryVO);
		if (count == 0) {
			return Collections.emptyList();
		} else {
			page.setTotalItem(count);
			return receivablesDetailDAO.queryPage(page, queryVO);
		}
	}

	@Override
	public List<ReceivablesDetailDO> queryByReceivablesId(Integer receivablesId) throws ManagerException {
		return receivablesDetailDAO.queryByReceivablesId(receivablesId);
	}

}
